<template>
  <div class="app-container">
    <div class="filter-container">
      <el-form ref="filterForm" :model="filterForm" :inline="true" label-position="left" label-width="100px">
        <el-form-item label="媒体" prop="keyword">
          <el-input v-model="filterForm.keyword" placeholder="媒体ID/媒体名称" style="width: 200px;" class="filter-item" @keyup.enter.native="handleFilter" />
        </el-form-item>
        <el-form-item label="广告位" prop="location">
          <el-input v-model="filterForm.location" placeholder="广告位ID/广告位名称" style="width: 200px;" class="filter-item" @keyup.enter.native="handleFilter" />
        </el-form-item>
        <el-form-item label="有效期" prop="keyword">
          <el-date-picker 
            v-model="filterForm.happenDate"
            type="datetimerange"
            value-format="yyyy-MM-dd"
            range-separator="至"
            start-placeholder="开始日期"
            end-placeholder="结束日期">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="状态" prop="status">
            <el-select v-model="filterForm.status" placeholder="请选择">
              <el-option label="0" value="0">全部</el-option>
              <el-option label="1" value="1">已结算</el-option>
              <el-option label="2" value="2">未结算</el-option>
            </el-select>
        </el-form-item>
        <el-form-item>
        <el-button v-waves class="filter-item" type="primary" icon="el-icon-search" @click="handleFilter">
          搜索
        </el-button>
        <el-button v-waves class="filter-item" type="primary" @click="handleCreate()">
          新推送
        </el-button>
        </el-form-item>
      </el-form>
    </div>

    <el-table
      :key="tableKey"
      v-loading="listLoading"
      :data="list"
      border
      fit
      highlight-current-row
      style="width: 100%;"
      @sort-change="sortChange"
      @selection-change="handleSelectionChange"
    >
      >
      <el-table-column
        type="selection"
        width="55"
      />
      <el-table-column label="编号" max-width="150px">
        <template slot-scope="{row}">
          <span class="link-type" @click="handleRead(row)">{{ row.code }}</span>
        </template>
      </el-table-column>
      <el-table-column label="位置" max-width="150px">
        <template slot-scope="{row}">
          <span class="link-type" @click="handleLocation(row.location)">{{ row.location.title }}</span>
        </template>
      </el-table-column>
      <el-table-column label="资源" max-width="150px">
        <template slot-scope="{row}">
          <span class="link-type" @click="handleBlackboard(row.blackboard)">{{ row.blackboard.title }}</span>
        </template>
      </el-table-column>
      <el-table-column label="开始时间" max-width="140px">
        <template slot-scope="{row}">
          <span>{{ row.begin_time }}</span>
        </template>
      </el-table-column>
      <el-table-column label="结束时间" max-width="140px">
        <template slot-scope="{row}">
          <span>{{ row.end_time }}</span>
        </template>
      </el-table-column>
      <el-table-column label="点击量" min-width="50px">
        <template slot-scope="{row}">
          <span>{{ row.clickNum }}</span>
        </template>
      </el-table-column>
      <el-table-column label="展示量" min-width="50px">
        <template slot-scope="{row}">
          <span>{{ row.viewNum }}</span>
        </template>
      </el-table-column>
      <el-table-column label="转化率" min-width="50px">
        <template slot-scope="{row}">
          <span>{{ row.orderAmount }}</span>
        </template>
      </el-table-column>
      <el-table-column label="状态" class-name="status-col" width="100">
        <template slot-scope="{row}">
          <el-tag :type="row.status | statusFilter">
            {{ row.status.label }}
          </el-tag>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" width="230" class-name="small-padding fixed-width">
        <template slot-scope="{row}">
          <el-button size="mini" type="success" @click="handleUpdate(row)">
            修改
          </el-button>
          <el-button v-if="row.status.value === 5 || row.status.value === 1" size="mini" @click="handleModifyStatus(row,6)">
            启用
          </el-button>
          <el-button v-if="row.status.value === 6" size="mini" type="danger" @click="handleModifyStatus(row,5)">
            禁用
          </el-button>
          <el-button v-if="row.status.value == 1 || row.status.value === 4 || row.status.value == 2 || row.status.value == 5" size="mini" type="danger" @click="handleRemove(row)">
            删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total>0" :total="total" :page.sync="filterForm.start" :limit.sync="filterForm.perpage" @pagination="getList" />

  </div>
</template>

<script>
import { getPropellings, removePropellingByID, changePropellingStatusByID } from '@/api/popularize/propelling'
import waves from '@/directive/waves' // waves directive
import { parseTime } from '@/utils'
import Pagination from '@/components/Pagination' // secondary package based on el-pagination

export default {
  name: 'ComplexTable',
  components: { Pagination },
  directives: { waves },
  filters: {
  },
  data() {
    return {
      tableKey: 0,
      list: null,
      total: 0,
      listLoading: true,
      filterForm: {
        start: 1,
        perpage: 10,
        keyword: undefined,
        location_identity: undefined,
        blackboard_identity: undefined,
        type: undefined,
        launch_time: undefined,
        sort: '+id'
      },
      sortOptions: [{ label: 'ID Ascending', key: '+id' }, { label: 'ID Descending', key: '-id' }]
    }
  },
  created() {
    this.getList()
  },
  methods: {
    handeUpdate() {

    },
    getList() {
      this.listLoading = true
      getPropellings(this.filterForm).then(response => {
        this.list = response.data.list
        this.total = response.data.total

        // Just to simulate the time of the request
        setTimeout(() => {
          this.listLoading = false
        }, 1.5 * 1000)
      })
    },
    handleSelectionChange(val) {
      this.multipleSelection = val
    },
    handleFilter(from = 0) {
      from = parseInt(from)
      if (!isNaN(from) && from < 1) {
        this.resetListQuery()
      }
      this.filterForm.start = 1
      this.getList()
    },
    handleLocation(location) {
      this.filterForm.location_identity = location.ID
      this.handleFilter(1)
    },
    handleBlackboard(blackboard) {
      this.filterForm.blackboard_identity = blackboard.ID
      this.handleFilter(1)
    },
    resetListQuery() {
      this.filterForm = {
        start: 1,
        perpage: 10,
        keyword: undefined,
        location_identity: undefined,
        blackboard_identity: undefined,
        type: undefined,
        launch_time: undefined,
        sort: '+id'
      }
    },
    handleModifyStatus(row, status) {
      const _this = this
      const formData = {
        'propellingID': row.ID,
        'status': status
      }
      changePropellingStatusByID(formData).then(response => {
        if (response.status === 200) {
          this.$message({
            message: '操作Success',
            type: 'success'
          })
          // _this.filterForm.start = 1
          _this.getList()
        } else {
          this.$message({
            message: response.msg,
            type: 'error'
          })
        }
      })
    },
    sortChange(data) {
      const { prop, order } = data
      if (prop === 'id') {
        this.sortByID(order)
      }
    },
    sortByID(order) {
      if (order === 'ascending') {
        this.filterForm.sort = '+id'
      } else {
        this.filterForm.sort = '-id'
      }
      this.handleFilter()
    },
    resetTemp() {
      this.temp = {
        id: undefined,
        content: '',
        receive: '',
        send_time: '',
        title: '',
        status: '1'
      }
    },
    handleCreate() {
      this.$router.push({ path: 'propelling-add' })
    },
    handleUpdate(item) {
      this.$router.push({ path: 'propelling-add', query: { 'propellingID': item.ID }})
    },
    handleRemove(row, index) {
      removePropellingByID({ 'propellingID': row.ID }).then(response => {
        console.log(response)
        this.$notify({
          title: 'Success',
          propelling: 'Delete Successfully',
          type: 'success',
          duration: 2000
        })
        this.filterForm.start = 1
        this.getList()
      })
    },
    formatJson(filterVal) {
      return this.list.map(v => filterVal.map(j => {
        if (j === 'send_time') {
          return parseTime(v[j])
        } else {
          return v[j]
        }
      }))
    },
    getSortClass: function(key) {
      const sort = this.filterForm.sort
      return sort === `+${key}` ? 'ascending' : 'descending'
    }
  }
}
</script>
